package fr.utbm.aiechat.structures;

import java.io.Serializable;
import java.util.ArrayList;


public class Channel implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int UniqueID;
	private User Admin;
	private int Port;
	private String Name;
	private boolean Private;
	private ArrayList<Message> message = new ArrayList<Message>();
	private ArrayList<User> user = new ArrayList<User>();
	public Channel(int UniqueID, User Admin, int Port, String Name, boolean Private)
	{
		this.UniqueID = UniqueID;
		this.Admin = Admin;
		this.Name = Name;
		this.Private = Private;
	}
	
	public int getUniqueID()
	{
		return UniqueID;
	}
	
	public User getAdmin()
	{
		return Admin;
	}
	
	public int getPort()
	{
		return Port;
	}
	
	public boolean isMyChan()
	{
		return (Admin == null);
	}
	
	public String getName()
	{
		return Name;
	}
	
	public boolean isPrivate()
	{
		return Private;
	}
	public void addMessage(Message mess) {
		this.message.add(mess);
	}

	public void setListMessage(ArrayList<Message> message) {
		this.message = message;
	}

	public ArrayList<Message> getListMessage() {
		return message;
	}

	public void setListUser(ArrayList<User> user) {
		this.user = user;
	}

	public ArrayList<User> getListUser() {
		return user;
	}
	public void addUser(User user) {
		if (!containsUser(user))
			this.user.add(user);	
	}
	public boolean containsUser(User user) {
		boolean trouver = false;
		for (int i=0 ; i < this.user.size() ; i++) {
			trouver = this.user.get(i).getAddress().toString().equals(user.getAddress().toString());
			if (trouver == true) {
				break;
			}
		}
		return trouver;
	}
	public int getPositionUserByUser(User user) {
		boolean trouver = false;
		for (int i=0 ; i < this.user.size() ; i++) {
			trouver = this.user.get(i).getAddress().toString().equals(user.getAddress().toString());
			if (trouver == true) {
				return i;
			}
		}
		return -1;
	}

	public void removeUserByUser(User userLeft) {
		int position = getPositionUserByUser(userLeft);
		if (position != -1)
			this.user.remove(position);
	}
}
